package com.fjl.utils;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * TOOD
 *
 * @author luo
 * @version 1.0
 * @date 2020/5/26 14:55
 */
public class FileUtils {
    public static boolean uploadFile(byte[] aryFileBytes,String filePath){
        boolean bRet = true;
       try {
           File classPathDirectory = null;
           classPathDirectory = new File(ResourceUtils.getURL("classpath:").getPath());
           if(!classPathDirectory.exists())
               classPathDirectory = new File("");
//           filePath = filePath.substring(0,filePath.lastIndexOf("/")+1);
           File uploadDirectory = new File(classPathDirectory.getAbsolutePath(),filePath);
           if(!uploadDirectory.exists()){
               uploadDirectory.getParentFile().mkdir();
           }
           if (!uploadDirectory.getParentFile().exists()){
               uploadDirectory.getParentFile().mkdirs();
               uploadDirectory.createNewFile();
//               uploadDirectory.mkdirs();
               System.out.println(uploadDirectory.exists());
           }

           FileOutputStream out = new FileOutputStream(classPathDirectory.getAbsolutePath()+"\\"+filePath);
           out.write(aryFileBytes);
           out.flush();
           out.close();

       } catch (FileNotFoundException e) {
           e.printStackTrace();
           bRet = false;
       } catch (IOException e) {
           e.printStackTrace();
           bRet = false;
       }

        return bRet;
    }
}
